package com.kkkitsch.coolalbum.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Encoder;

public class ImageClientUtil {

	public static String validationImg(String image) throws Exception {

		// 客户端对象
		CloseableHttpClient httpclient = getCloseableHttpClient();

		// 百度云头像审核url
		String txurl = "https://aip.baidubce.com/rest/2.0/solution/v1/face_audit?access_token="
				+ ImageAuthService.getAuth();

		// POST请求和头像审核内容格式
		HttpPost httpPost = new HttpPost(txurl);
		httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

		// 请求参数设置
		List<BasicNameValuePair> nameValuePairlist = new ArrayList<BasicNameValuePair>();
		// base64字符串图片
		nameValuePairlist.add(new BasicNameValuePair("images", image));
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairlist, "UTF-8");
		httpPost.setEntity(formEntity);

		// 请求发送
		CloseableHttpResponse response = httpclient.execute(httpPost);

		// 响应
		HttpEntity entity = response.getEntity();
		String respContent = EntityUtils.toString(entity, "UTF-8").trim();
		httpPost.abort();
		return respContent;
	}

	public static String validation(MultipartFile file) throws Exception {
		InputStream inputStream = null;
		byte[] data = null;
		try {
			inputStream = file.getInputStream();
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 加密
		BASE64Encoder encoder = new BASE64Encoder();
		String encodeImgString = encoder.encode(data);
		String validContent = validationImg(encodeImgString);

		String substring = validContent.substring(validContent.indexOf("res_code"),
				validContent.indexOf("res_code") + 11);
		return substring;
	}

	public static CloseableHttpClient getCloseableHttpClient() {
		// 客户端对象
		CloseableHttpClient httpclient = null;
		try {
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
			// 创建一个可以访问https的请求对象
			httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			return httpclient;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}